package com.hyper_yang._05_juc.chap08;

import java.util.concurrent.*;

/**
 * 演示Executors创建线程池(了解，不允许使用)
 */
public class ExecutorsDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService threadPool = Executors.newSingleThreadExecutor(); //一池一线程
        //ExecutorService threadPool = Executors.newFixedThreadPool(3); //指定固定线程数量的线程池
        //ExecutorService threadPool = Executors.newCachedThreadPool(); //自带伸缩能力的线程池。任务越多创建线程越多。遇强则强。

        //Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "pool-1-thread-16"

        //execute方法只能接收Runnable对象
//        for (int i = 0; i < 10000000; i++) {
//            final int j = i;
//            threadPool.execute(new Runnable() {
//                @Override
//                public void run() {
//                    System.out.println(Thread.currentThread().getName() + "\t" + "---come in "+ j);
//                }
//            });
//        }

        //submit方法可以接收Runnable或Callable
        Future<Integer> future = threadPool.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return 111;
            }
        });
        System.out.println("future.getClass().getName() = " + future.getClass().getName());
        Integer result = future.get(); //阻塞方法
        System.out.println("result = " + result);

        //threadPool.shutdown();
    }
}
